#include "GenManItem.h"

GenManItem::GenManItem(QWidget *parent):
	QWidget(parent)
{
	SetupUI();
	Initialize();
	MakeConnections();
}

GenManItem::~GenManItem()
{
}

std::string GenManItem::getIP()
{
	return m_genConfig.ip;
}

int GenManItem::getGenId()
{
    return m_genId;
}

void GenManItem::setGenId(int genId)
{
    m_genId = genId;
}

void GenManItem::MakeConnections()
{
	QObject::connect(lineEdit, SIGNAL(editingFinished()), this, SLOT(IpHandle()));
	QObject::connect(lineEdit_2, SIGNAL(editingFinished()), this, SLOT(CountHandle()));
	QObject::connect(pushButton, SIGNAL(clicked()), this, SLOT(ConnectHandle()));
	QObject::connect(pushButton_2, SIGNAL(clicked()), this, SLOT(DelGenHandle()));
}


void GenManItem::IpHandle()
{
	// split
	QLineEdit *sender = (QLineEdit *)this->sender();
	QString text = sender->text();
	QString genIP(m_genConfig.ip.c_str());

	QStringList list = text.split('.', QString::SkipEmptyParts);
	if(text != "localhost" && list.count() != 4) return;
	if(text == genIP) return; // ip doesn't change
	m_genConfig.ip = text.toStdString();
	emit genConfigChanged(m_genId, m_genConfig);
}

void GenManItem::CountHandle()
{
	QLineEdit *sender = (QLineEdit *)this->sender();
	int count = sender->text().toInt();
	if(count != m_genConfig.userCount){
		m_genConfig.userCount = count;
		emit genConfigChanged(m_genId, m_genConfig);
	}
}

void GenManItem::DelGenHandle()
{
	emit delGen(m_genId);
}

void GenManItem::ConnectHandle()
{
}

void GenManItem::ConnectDoneHandle(const QString&, int status)
{
}

void GenManItem::Initialize()
{
	m_genConfig.ip = "";
	m_genConfig.userCount = 0;
	m_genConfig.isOn = false;

	lineEdit->setText(QString(m_genConfig.ip.c_str()));

	label->setText("down");

	QString str;
	str.setNum(m_genConfig.userCount);
	lineEdit_2->setText(str);
}

void GenManItem::SetupUI()
{
	horizontalLayout = new QHBoxLayout(this);
	horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
	horizontalLayout->setContentsMargins(0, 0, 0, 0);
	lineEdit = new QLineEdit(this);
	lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
	lineEdit->setFixedWidth(120); // adjust ip area length

	horizontalLayout->addWidget(lineEdit);

	label = new QLabel(this);
	label->setObjectName(QString::fromUtf8("label"));
	label->setFixedWidth(50); // adjust count length

	horizontalLayout->addWidget(label);

	lineEdit_2 = new QLineEdit(this);
	lineEdit_2->setObjectName(QString::fromUtf8("lineEdit_2"));
	lineEdit_2->setFixedWidth(40);

	horizontalLayout->addWidget(lineEdit_2);

	pushButton = new QPushButton(this);
	pushButton->setFixedWidth(20);
	pushButton->setObjectName(QString::fromUtf8("pushButton"));

	horizontalLayout->addWidget(pushButton);

	pushButton_2 = new QPushButton(this);
	pushButton_2->setFixedWidth(20);
	pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));

	horizontalLayout->addWidget(pushButton_2);
	horizontalLayout->addStretch();

	pushButton->setText(QApplication::translate("Form", "C", 0, QApplication::UnicodeUTF8));
	pushButton_2->setText(QApplication::translate("Form", "D", 0, QApplication::UnicodeUTF8));
}
